package com.tramy.data.export.args;

import com.tramy.data.export.engine.DataEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by honway on 2016/11/3.
 */
@Component
public class CommandLineArgsParse implements CommandLineRunner {

    @Autowired
    private DataEngine customerDataEngine;

    @Autowired
    private DataEngine lineDataEngine;

    @Autowired
    private DataEngine checkoutCustomerDataEngine;

    @Autowired
    private DataEngine workshopDataEngine;

    @Autowired
    private DataEngine employeeDataEngine;

    @Override
    public void run(String... strings) throws Exception {
        System.out.println("开始处理命令行参数.");
        List<String> argList = new ArrayList<>();
        for (String string : strings) {
            argList.add(string);
        }
        if (argList.size() != 0) {
            if (argList.size() == 1) {
                String args = argList.get(0);
                if ("customer".equalsIgnoreCase(args)) {

                    System.out.println("-------开始导入客户数据.-------");
                    customerDataEngine.justDoIt();
                    System.out.println("-------客户数据处理完毕.-------");

                } else if ("line".equalsIgnoreCase(args)) {

                    System.out.println("-------开始导入线路数据.-------");
                    lineDataEngine.justDoIt();
                    System.out.println("-------线路数据处理完毕.-------");

                } else if ("workshop".equalsIgnoreCase(args)) {

                    System.out.println("-------开始处理生产组数据.-------");
                    workshopDataEngine.justDoIt();
                    System.out.println("-------生产组数据处理完毕-------");

                } else if ("checkoutCustomer".equalsIgnoreCase(args)) {

                    System.out.println("-------开始处理结账客户数据.-------");
                    checkoutCustomerDataEngine.justDoIt();
                    System.out.println("-------结账客户数据处理完毕.-------");

                } else if ("employee".equalsIgnoreCase(args)) {

                    System.out.println("-------开始处理员工数据.-------");
                    employeeDataEngine.justDoIt();
                    System.out.println("-------员工数据处理完毕.-------");

                } else {
                    System.out.println(help());
                    System.exit(100);
                }
            } else {
                System.out.println("只能接受一个参数.\n");
                System.out.println(help());
                System.exit(200);
            }
        } else {
            System.out.println("没有接收到任何参数.");
            System.out.println(Arrays.toString(argList.toArray()));
            System.out.println(help());
            System.exit(-1);
        }
    }

    private String help() {
        return "\n\n" +
                "========================================================\n\n" +
                "customer           --从新烽火中导入客户信息到新清美中.\n" +
                "line               --从新立成中导入线路信息到新清美中.\n" +
                "workshop           --从新立成中导入生产组信息到新清美中.\n" +
                "checkoutCustomer   --从新立成中导入结账客户到新清美中.\n" +
                "employee           --从新立成中导入职员信息到新清美中.\n\n" +
                "========================================================";
    }
}
